//
//  NovelDataBase.m
//  DataBase
//
//  Created by dllo on 15/8/14.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MedicineBoxDataBase.h"
#import "DB.h"
#import "HomeModel.h"



@implementation MedicineBoxDataBase

+ (BOOL)insertWithModel:(HomeModel *)model
{
    // 1. 获得数据库指针
    sqlite3 *db = [DB dbOpen];
    // 2. 获得数据库语句
    NSString *str = [NSString stringWithFormat:@"insert into Drug(namecn,titleimg,avgprice,c_id,type) values('%@','%@','%@','%ld','%@')",model.namecn,model.titleimg,model.avgprice,(long)[model._id integerValue],model.type];
    // 3. 执行sql语句
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    NSLog(@"s = %d",result);
    if (result == SQLITE_OK) {
        NSLog(@"insert 成功");
        return YES;
    }
    else {
        NSLog(@"insert 失败");
        return NO;
    }
}



//查询: 1查询所有的 2.查询单个的
+ (NSArray *)selectAll
{
    //1.获得数据库指针
    sqlite3 *db = [DB dbOpen];
    //2.定义数据库替身
    sqlite3_stmt *stmt = nil;
    NSString *str = [NSString stringWithFormat:@"select namecn,titleimg,avgprice,c_id,type from Drug"];
    //3.执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSLog(@"%d", result);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        //判断是否有下一行数据,可用于读取
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 0);
            const unsigned char *titleimg = sqlite3_column_text(stmt, 1);
            const unsigned char *price = sqlite3_column_text(stmt, 2);
            const unsigned char *c_id = sqlite3_column_text(stmt, 3);
            const unsigned char *type = sqlite3_column_text(stmt, 4);
             NSString *namecn = [NSString stringWithUTF8String:(const char *)name];
             NSString *img = [NSString stringWithUTF8String:(const char *)titleimg];
             NSString *avg = [NSString stringWithUTF8String:(const char *)price];
             NSString *ID = [NSString stringWithUTF8String:(const char *)c_id];
            NSNumber *avgprice = [MedicineBoxDataBase stringFormatOfnumberWithString:avg];
            NSNumber *drugId = [MedicineBoxDataBase stringFormatOfnumberWithString:ID];
            NSString *myType = [NSString stringWithUTF8String:(const char *)type];
            HomeModel *model = [HomeModel medicineBoxWithName:namecn titleimg:img avgprice:avgprice drug:drugId type:myType];
            [array addObject:model];
        }
        NSLog(@"成功");
    }else
    {
        NSLog(@"失败");
    }
    sqlite3_finalize(stmt);
    return array;

}
+ (HomeModel *)selectOnlyWith:(NSString *)sId
{
    //1.获得数据库指针
    sqlite3 *db = [DB dbOpen];
    //2.定义数据库替身
    sqlite3_stmt *stmt = nil;
    NSString *str = [NSString stringWithFormat:@"select * from Drug where c_id = '%@'",sId];
    //3.执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSLog(@"%d", result);
       if (result == SQLITE_OK) {
        //判断是否有下一行数据,可用于读取
       if (sqlite3_step(stmt) == SQLITE_ROW) {
           const unsigned char *name = sqlite3_column_text(stmt, 0);
           const unsigned char *titleimg = sqlite3_column_text(stmt, 1);
           const unsigned char *price = sqlite3_column_text(stmt, 2);
           const unsigned char *c_id = sqlite3_column_text(stmt, 3);
           const unsigned char *type = sqlite3_column_text(stmt, 4);
           NSString *namecn = [NSString stringWithUTF8String:(const char *)name];
           NSString *img = [NSString stringWithUTF8String:(const char *)titleimg];
           NSString *avg = [NSString stringWithUTF8String:(const char *)price];
           NSString *ID = [NSString stringWithUTF8String:(const char *)c_id];
           NSNumber *avgprice = [MedicineBoxDataBase stringFormatOfnumberWithString:avg];
           NSNumber *drugId = [MedicineBoxDataBase stringFormatOfnumberWithString:ID];
           NSString *myType = [NSString stringWithUTF8String:(const char *)type];
           HomeModel *model = [HomeModel medicineBoxWithName:namecn titleimg:img avgprice:avgprice drug:[NSString stringWithFormat:@"%@", drugId] type:myType];           sqlite3_finalize(stmt);
           return model;
        }
        NSLog(@"成功");
    }else
    {
        NSLog(@"失败");
    }
    sqlite3_finalize(stmt);
    return nil;
    
}

+ (void)deleteWith:(HomeModel *)model
{
    // 获取指针
    
     sqlite3 *db = [DB dbOpen];
    
    // 获取数据库语句
    NSString *str = [NSString stringWithFormat:@"delete from Drug where c_id = '%@'", model._id];
    
    // 执行数据库语句
    int reuselts = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (reuselts == SQLITE_OK) {
        NSLog(@"delete成功");
    }
    else {
        NSLog(@"delete失败");
    }

}
// 字符串转化为NSNumber
+ (NSNumber *)stringFormatOfnumberWithString:(NSString *)string
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *num = [numberFormatter numberFromString:string];
    return num;
}











@end
